//
//  ViewController.m
//  AppOne
//
//  Created by mac on 2019/2/25.
//  Copyright © 2019 YangYang. All rights reserved.
//

#import "ViewController.h"


static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";

@interface ViewController ()
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 40)];
    [self.view addSubview:label];
    label.text = @"第一个APP";
    label.font = [UIFont systemFontOfSize:20];
    label.textColor = [UIColor lightGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button setTitle:@"跳转至 AppTwo" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(100, CGRectGetMaxY(label.frame) + 100, [UIScreen mainScreen].bounds.size.width - 200, 40);
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    [self.view addSubview:self.containerView];
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.player.controlView = self.controlView;
    self.player.assetURLs = @[
                              [NSURL URLWithString:@"https://www.apple.com/105/media/cn/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/bruce/mac-bruce-tpl-cn-2018_1280x720h.mp4"],
                              [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"]],
                              [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp4"]],
                              [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"5" ofType:@"mp4"]],
                              [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"3" ofType:@"mp4"]],
                              [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"4" ofType:@"mp4"]]];
    __weak typeof(self) weakSelf = self;
    self.player.playerReadyToPlay = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset, NSURL * _Nonnull assetURL) {
        NSLog(@"开始播放了");
    };
    self.player.playerDidToEnd = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
        NSLog(@"播放结束了");
        [weakSelf.player stop];
    };
    
    
    
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
    }
    return _controlView;
}

- (UIImageView *)containerView {
    if (!_containerView) {
        _containerView = [UIImageView new];
        _containerView.frame = CGRectMake(0, 200, kScreenWidth, 250);
        [_containerView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:kVideoCover]]]];
    }
    return _containerView;
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([self.player isLastAssetURL]) {
        NSLog(@"是最后一个---------%ld-----",self.player.currentPlayIndex);
        [self.player playTheIndex:0];
    }
    else
    {
        NSLog(@"不是最后一个---------%ld-----",self.player.currentPlayIndex);
        [self.player playTheNext];
    }
}

-(void)buttonAction:(UIButton *)sender
{
    NSLog(@"跳转到第二个APP");
    NSURL *myurl = [NSURL URLWithString:@"AppTwo://openBSecondPage"];
    if ([[UIApplication sharedApplication] canOpenURL:myurl]) {
        NSLog(@"可以打开第二个APP");
        [[UIApplication sharedApplication] openURL:myurl options:@{} completionHandler:nil];
    } else {
        NSLog(@"打开第二个APP失败");
    }
}


@end
